package com.example.android.commitcontent.ime.setting;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.android.commitcontent.ime.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SettingAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<SettingBean> mSettingBeans;
    private OnItemClickListener mOnItemClickListener;
    private OnItemLongClickListener mOnItemLongClickListener;

    private final int TYPE_TITLE = 88;
    private final int TYPE_ITEM = 99;

    public SettingAdapter(Context context, List<SettingBean> settingBeans) {
        mContext = context;
        mSettingBeans = settingBeans;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_TITLE) {
            View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_setting_title, parent, false);
            return new TitleHolder(inflate);
        }
        final SettingHolder settingHolder = new SettingHolder(LayoutInflater.from(mContext).inflate(R.layout.item_setting, parent, false));
        settingHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = settingHolder.getAdapterPosition();
                int realPosition = position;
                for (int i = 0; i < position; i++) {
                    if (mSettingBeans.get(i).isTitle()) {
                        realPosition--;
                    }
                }
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(realPosition);
                }
            }
        });
        settingHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                int position = settingHolder.getAdapterPosition();
                int realPosition = position;
                for (int i = 0; i < position; i++) {
                    if (mSettingBeans.get(i).isTitle()) {
                        realPosition--;
                    }
                }
                if (mOnItemLongClickListener != null) {
                    mOnItemLongClickListener.onItemLongClick(realPosition);
                }
                return true;
            }
        });
        return settingHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        SettingBean settingBean = mSettingBeans.get(position);
        if (holder instanceof TitleHolder) {
            ((TitleHolder) holder).mTvTitle.setText(settingBean.getTitle());
        } else if (holder instanceof SettingHolder) {
            ((SettingHolder) holder).mTvTitle.setText(settingBean.getTitle());
            ((SettingHolder) holder).mTvDesc.setText(settingBean.getDesc());
            ((SettingHolder) holder).mTvDesc.setVisibility(TextUtils.isEmpty(settingBean.getDesc()) ? View.GONE : View.VISIBLE);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return mSettingBeans.get(position).isTitle() ? TYPE_TITLE : TYPE_ITEM;
    }

    @Override
    public int getItemCount() {
        return mSettingBeans.size();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        mOnItemLongClickListener = onItemLongClickListener;
    }

    public class TitleHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_title)
        TextView mTvTitle;

        public TitleHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public class SettingHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_title)
        TextView mTvTitle;
        @BindView(R.id.tv_desc)
        TextView mTvDesc;

        public SettingHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    public interface OnItemLongClickListener {
        void onItemLongClick(int position);
    }
}


